import React, { useEffect, useState } from "react";
import { HashRouter as Router,Route ,Switch} from "react-router-dom";
import { Link } from "react-router-dom";
function Child1() {
  return (
    <div>
      <h2>
        hello world child1
      </h2>
    </div>
  )
}
function Child2() {
  return (
    <div>
      <h2>
        child2
      </h2>
    </div>
  )
}


export default function LifeCycleExample() {
  const [cnt , setCnt] = useState(0)
  useEffect(()=>{
    //useEffect 不影响视图更新，异步延迟执行，不是同步的
    console.log(`component did update, UI 更新了 全局UI更新 , cnt := ${cnt}`)
    return ()=> {
      console.log('----')
    }
  })

  const resize = ()=>{
    console.log('windows resize ')
  }

  useEffect(()=>{
    window.addEventListener('resize', resize)
    return () => window.removeEventListener('resize', resize)
  },[])

  useEffect(()=>{
    //useEffect 不影响视图更新，异步延迟执行，不是同步的
    console.log(`cnt 被更新了, cnt := ${cnt}`)
    return ()=> {
      console.log(' --- end cnt ---- ')
    }
  },[cnt])


  return (
    <div>
      
      <Router >
        <button onClick={()=>setCnt(cnt+1)}>
          点击 {cnt}
        </button>
        11
        <br />
        <Link to="./" >child1</Link>
        <br />
        <Link to="child2" >child2</Link>
        <Switch>
          <Route path="/LifeCycle/child2" exact component={Child2} />
          <Route path="/LifeCycle/child1" exact component={Child1} />
          <Route path="/LifeCycle/" exact component={Child1} />
        </Switch>
        
      </Router>
    </div>
  )
}